package org.example.concole;

import io.netty.channel.Channel;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

// 配置命令

// 执行命令
public class ConsoleCommandManager implements ConsoleCommand{

    private Map<String, ConsoleCommand> consoleCommandMap;

    public ConsoleCommandManager() {
        consoleCommandMap = new HashMap<>();
        consoleCommandMap.put("sendToUser", new SendToUserConsoleCommand());
        consoleCommandMap.put("logout", new LogoutConsoleCommand());
        consoleCommandMap.put("createGroup", new CreateGroupConsoleCommand());
        consoleCommandMap.put("joinGroup", new JoinGroupConsoleCommand());
        consoleCommandMap.put("quitGroup", new QuitGroupConsoleCommand());
        consoleCommandMap.put("listMembers", new MemberGroupConsoleCommand());
    }

    @Override
    public void exec(Scanner scanner, Channel channel) {
        System.out.println("您已登陆，现在可以进行发送消息、创建群组了，请输入指令：sendToUser | createGroup | joinGroup | quitGroup | listMembers:");
        // 获取第一个输入（代表操作的指令）
        String command = scanner.next();

        // 获取ConsoleCommand对象
        ConsoleCommand consoleCommand = consoleCommandMap.get(command);

        // 执行
        if (consoleCommand != null) {
            consoleCommand.exec(scanner, channel);
        }else {
            System.err.println("无法识别[" + command + "]指令，请重新输入！");
        }
    }

}
